package com.youth.mq.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;

import java.io.IOException;

/**
 * @ClassName LogRMQConsumer
 * @Description: TODO
 * @Author yjwu
 * @Date 2020/4/9 12:01
 **/
@RabbitListener(bindings = @QueueBinding(
        exchange = @Exchange(value = "${logRMQExchangeName}", durable = "true", type = "topic"),
        value = @Queue(value = "${logRMQQueueName}", durable = "true"),
        key = "${logRMQRoutingKey}"
))
@Slf4j
public class LogRMQConsumer {

    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws IOException {
        // 限流处理：消息体大小不限制，每次限制消费一条，只作用于该Consumer层    ，不作用于Channel
//        channel.basicQos(0, 1, false);//限制于消费级别
        String s = new String(message.getBody());
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        log.info("LogRMQConsumer receive msg:{},deliveryTag：{}", s, deliveryTag);
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("e.getmessage:{}", e.getMessage());
            channel.basicReject(deliveryTag, false);
        }
    }

}
